package com.jade.zsy.controller.base.api;

import com.jade.bss.base.RoleAction;
import com.jade.bss.web.resolver.CustomerCapsule;
import com.jade.bss.base.utils.CopyBean;
import com.jade.framework.base.context.ApplicationContextUtils;
import com.jade.framework.base.util.PagingResult;
import com.jade.framework.base.util.ResponseUtils;
import com.jade.zsy.base.common.ParametersException;
import com.jade.zsy.base.entity.Org;
import com.jade.zsy.base.facade.StoreFacade;
import com.jade.zsy.base.vo.StoreVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/6/5
 * Time: 15:17
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@RestController
@RequestMapping(value = "/store", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "店铺管理", description = "基础模块--店铺管理")
public class StoreApi {

    private StoreFacade storeFacade = ApplicationContextUtils.getBean("StoreFacade");
    @ResponseBody
    @RequestMapping(value =  RoleAction.QUERY, method = RequestMethod.GET)
    @ApiOperation(value = "店铺管理", httpMethod = "GET", response = StoreVo.class)
    public void query(@ApiParam(value = "起始值") @RequestParam(value = "offset", defaultValue = "0") int offset,
                      @ApiParam(value = "每页大小") @RequestParam(value = "length", defaultValue = "10") int length,
                      @ApiParam(value = "语言标识") @RequestParam(value = "flag", defaultValue = "-1") int flag,
                      @ApiParam(value = "关键词") @RequestParam (value = "keyword", required = false) String keyword,
                      @ApiParam(value = "排序字段") @RequestParam(value = "sort", required = false) String sort,
                      @ApiParam(value = "排序规则", allowableValues = "desc, asc") @RequestParam(value = "order", required = false) String order,
                      HttpServletResponse response, StoreVo storeVo) throws Exception {
        PagingResult result = storeFacade.queryEntity(storeVo, offset, length, keyword, sort);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

    //@RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = RoleAction.ADD, method = RequestMethod.POST)
    @ApiOperation(value = "新增店铺", httpMethod = "POST")
    public void add(@ApiParam(value = "店铺对象", required = true) @Validated @RequestBody StoreVo storeVo,
                    Errors errors, HttpServletResponse response, CustomerCapsule capsule) throws Exception {
        if (errors.hasErrors()) {
            FieldError field = errors.getFieldError();
            throw new ParametersException(field.getDefaultMessage(), field.getDefaultMessage() + ":" + field.getField(), null, field.getField());
        }
        Org entity = CopyBean.copy(storeVo, Org.class);
        storeFacade.save(entity);
        ResponseUtils.writeSuccessResult(response);
    }

    //@RequiresAuthentication
    @ResponseBody
    @RequestMapping(value = RoleAction.UPDATE, method = RequestMethod.PUT)
    @ApiOperation(value = "修改店铺", httpMethod = "PUT")
    public void update(@ApiParam(value = "修改店铺", required = true) @Validated @RequestBody StoreVo storeVo,
                       Errors errors, HttpServletResponse response, CustomerCapsule capsule) throws Exception {
        if (errors.hasErrors()) {
            FieldError field = errors.getFieldError();
            throw new ParametersException(field.getDefaultMessage(), field.getDefaultMessage() + ":" + field.getField(), null, field.getField());
        }

        Org entity = CopyBean.copy(storeVo, Org.class);
        storeFacade.update(entity);
        ResponseUtils.writeSuccessResult(response);
    }
    //@RequiresAuthentication
    @ApiOperation(value = "店铺详情")
    @RequestMapping(value =RoleAction.GET, method = RequestMethod.GET)
    public void view(@ApiParam(value = "键值") @RequestParam(value = "id", defaultValue = "0") long id,HttpServletResponse response)throws Exception{
        Org entity = storeFacade.getById(Org.class, id);
        if (null == entity) {
            throw new ParametersException("err.module.record.not_existed", "record not existed");
        }
        StoreVo vo = CopyBean.copy(entity, StoreVo.class);
        ResponseUtils.writeSuccessResult(response, vo);
    }

    //@RequiresAuthentication
    @ResponseBody
    @RequestMapping(value =RoleAction.DELETE, method = RequestMethod.DELETE)
    @ApiOperation(value = "删除店铺", httpMethod = "DELETE")
    public void del(@ApiParam(value = "主键", required = true) @PathVariable(value = "id") Long[] id, HttpServletResponse response) throws Exception {
        storeFacade.delete(Org.class, id);
        ResponseUtils.writeSuccessResult(response);
    }
}
